package com.learn.egweibo2.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.learn.egweibo2.base.response.JsonResult;
import com.learn.egweibo2.base.response.StatusCode;
import com.learn.egweibo2.mapper.UserMapper;
import com.learn.egweibo2.pojo.dto.UserLoginParam;
import com.learn.egweibo2.pojo.dto.UserRegParam;
import com.learn.egweibo2.pojo.entity.User;
import com.learn.egweibo2.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/users/")
@Slf4j
@Api(tags = "01.用户管理模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("reg")
    @ApiOperation("注册功能")
    @ApiOperationSupport(order = 10)
    public JsonResult regUser(@Validated @RequestBody UserRegParam userRegParam){
        log.debug("userRegParam:"+userRegParam);
        //判断用户名是否被占用
        if(userMapper.selectByUsername(userRegParam.getUsername())>0){
            return new JsonResult(StatusCode.USERNAME_EXIST);
        }
        //添加数据
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        //将数据插入数据库
        log.debug("user:"+user);
        int res = userMapper.insertUser(user);
        log.debug(res>1?"注册成功":"注册失败");

        return JsonResult.ok();
    }

    @PostMapping("login")
    @ApiOperation("登录功能")
    @ApiOperationSupport(order = 20)
    public JsonResult loginUser(@RequestBody UserLoginParam userLoginParam, @ApiIgnore HttpSession session){
        log.debug("userLoginParam:" +userLoginParam);
        //查询用户名密码是否正确
        UserVO userVO = userMapper.selectUserId(userLoginParam);
        if(userVO==null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }

        log.debug("userVO" + userVO);
        //将用户id保存到session中
        session.setAttribute("user",userVO);

        return JsonResult.ok();
    }

    @GetMapping("currentUser")
    @ApiOperation("获取当前用户信息")
    @ApiOperationSupport(order = 30)
    public JsonResult getCurrentUserIngo(@ApiIgnore HttpSession session){
        //从session中获取用户id
        UserVO userVO = (UserVO) session.getAttribute("user");
        log.debug("userVO:"+userVO);
        //判断用户是否登录
        /*if(userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }*/

        return JsonResult.ok(userVO);
    }

    @GetMapping("logout")
    @ApiOperation("登出功能")
    @ApiOperationSupport(order = 40)
    public JsonResult logoutUser(@ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        log.debug("userVO:"+userVO);
        //判断用户是否登录
        /*if(userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }*/
        //登出
        session.removeAttribute("user");
        return JsonResult.ok();
    }
}
